//
//  SEGOrderSourceCell.m
//  Uhomebk
//
//  Created by mac on 2017/7/5.
//  Copyright © 2017年 com.segi. All rights reserved.
//

#import "SEGOrderSourceCell.h"
#import "SEGOrderDetailModel.h"

@implementation SEGOrderSourceCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.titleBgView.backgroundColor = kColor_W;
    self.detailBgView.backgroundColor = kColor_W;
    
    //初始化一些信息
    self.lineView.backgroundColor = kColor_L2;
    // 统一使用主题色
    self.isSuprviseLabel.backgroundColor = kColor_T;
    self.isSuprviseLabel.textColor = kColor_W;
    self.isSuprviseLabel.font = kFont_11;
    self.isSuprviseLabel.layer.cornerRadius = 2;
    self.isSuprviseLabel.layer.masksToBounds = YES;
    
    self.headTitleLabel.textColor = kColor_1;
    
    // 统一使用主题色
    self.isUrgentLabel.backgroundColor = kColor_T;
    self.isUrgentLabel.textColor = kColor_W;
    self.isUrgentLabel.font = kFont_11;
    self.isUrgentLabel.layer.cornerRadius = 2;
    self.isUrgentLabel.layer.masksToBounds = YES;
    
    self.isDealStatusLabel.backgroundColor = kColor_W;
    self.isDealStatusLabel.font = kFont_11;
    self.isDealStatusLabel.layer.cornerRadius = 2;
    self.isDealStatusLabel.layer.borderWidth = 0.5;
    self.isDealStatusLabel.layer.masksToBounds = YES;
    self.lineView.backgroundColor = kColor_L2;
    
    self.isDealStatusLabel.textColor = kColor_S2;
    self.isDealStatusLabel.layer.borderColor = kColor_S2.CGColor;
    self.isDealStatusLabel.textColor = kColor_T;
    self.isDealStatusLabel.layer.borderColor = kColor_T.CGColor;
    
    self.orderTypeTitleLabel.textColor = kColor_2;
    self.userNameTitleLabel.textColor = kColor_2;
    self.createTimeTitleLabel.textColor = kColor_2;
    self.organNameTitleLabel.textColor = kColor_2;
    self.orderIdTitleLabel.textColor = kColor_2;
    self.addressTitleLabel.textColor = kColor_2;
    
    self.orderIdLabel.textColor = kColor_1;
    self.orderDealNameLabel.textColor = kColor_1;
    self.orderTypeLabel.textColor = kColor_1;
    self.addressLabel.textColor = kColor_1;
    self.coummnityLabel.textColor = kColor_1;
    self.creatTimeLabel.textColor = kColor_1;
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

}


/**
 填充数据
 
 @param data
 */
-(void)paddingDataWithMod:(id)data
{

    SEGOrderDetailModel *mod = data;
    SEGOrderBaseInfoModel *baseInfo = mod.baseInfos;
    //工单节点标识
    if(![baseInfo.resultCodeName isEqualToString:@""]){
        self.isDealStatusLabel.hidden = NO;
        NSString *trackStr = baseInfo.resultCodeName;
        self.isDealStatusLabel.text = trackStr;
    }
    else{
        self.isDealStatusLabel.hidden = YES;
    }
    if ([baseInfo.resultCode isEqualToString:@"NODE_DS_OVER"] || [baseInfo.resultCode isEqualToString:@"NODE_DS_CANCEL"] || [baseInfo.resultCode isEqualToString:@"NODE_DS_CLOSE"])  {
        self.isDealStatusLabel.textColor = kColor_3;
        self.isDealStatusLabel.layer.borderColor = kColor_L2.CGColor;
    }
    //待督办 - 超时未督办
    NSString *supStatus =  [self checkIsSuperviseStatus:mod.baseInfos];
    supStatus = @""; // 不展示督办和超时标签

    //1.未超时 - 隐藏超时及督办标识
    if ([supStatus isEqualToString:@""]) {
        _isSuperviseLabelWidth.constant = 0;
        _urgentToIsSuperviseSpace.constant = 0;
    }
    //2.已超时未督办
    else if([supStatus containsString:@"超时"]){
        _isSuperviseLabelWidth.constant = 48;
        _isSuprviseLabel.text = supStatus;
        _urgentToIsSuperviseSpace.constant = 5;
    }
    //3.超时已督办
    else if([supStatus containsString:@"督办"])
    {
        _isSuprviseLabel.text = supStatus;
        _isSuperviseLabelWidth.constant = 48;
        _urgentToIsSuperviseSpace.constant = 5;
    }
    else{
        _isSuperviseLabelWidth.constant = 0;
        _urgentToIsSuperviseSpace.constant = 0;
    }
    _isSuperviseLabelWidth.constant = 0;
    _urgentToIsSuperviseSpace.constant = 0;
    
    //是否加急
    if ([baseInfo.urgentLevel isEqualToString:@"0"] || [baseInfo.urgentLevel isEqualToString:@""]) {
        _isUrgentLabel.hidden = YES;
    }
    else{
        _isUrgentLabel.hidden = NO;
        _isUrgentLabel.text = @"急";
    }
    
    //工单id
    self.orderIdLabel.text = baseInfo.serviceOrderId;
    
    //工单类型
    self.orderTypeLabel.text = baseInfo.templateInstName;
    
    //提单人
    NSString *userType;
    NSString *channel = baseInfo.transportTypeId;
    if ([channel isEqualToString:@"0"] || [channel isEqualToString:@"1"] || [channel isEqualToString:@"2"]) {
        userType=@"客户";
    }else if([channel isEqualToString:@"3"] || [channel isEqualToString:@"5"] || [channel isEqualToString:@"6"] || [channel isEqualToString:@"7"]){
        userType=@"系统";
    }else if([channel isEqualToString:@"4"]){
        userType=@"";
    }else{
        userType=@"";
    }
    if (userType.length == 0) {
       self.orderDealNameLabel.text=[NSString stringWithFormat:@"%@",baseInfo.contactName];
    }
    else{
       self.orderDealNameLabel.text=[NSString stringWithFormat:@"%@（%@）",baseInfo.contactName,userType];
    }
   
    if (![baseInfo.houseInfo isEqualToString:@""]) {
        self.addressLabel.text = baseInfo.houseInfo;
        self.addressTitleLabel.text = @"地址";
        self.addressTopToUserNameBottomSpace.constant = 15;
    }
    else{
        self.addressLabel.text = @"";
        self.addressTitleLabel.text = @"";
        self.addressTopToUserNameBottomSpace.constant = 0;
    }
    
    //申请时间
    self.creatTimeLabel.text = [SEGUtils getTimeWithTimeStr:baseInfo.createDate timeFormat:@"yyyy-MM-dd HH:mm"];

    //项目
    self.coummnityLabel.text = baseInfo.organName;
}

-(NSString *)checkIsSuperviseStatus:(SEGOrderBaseInfoModel *)model
{
    //    "supflag",""//处理过的环节，超时标识 1 ，2，3分别标识几级超时 0 或者空未超时
    //    "suptype",""//处理过的环节，超时处理 000：未处理，001：一级督办，011：二级督办，111：三级督办  非000或空时参考supflag。
    
    NSString *statusStr =@"";
    //督办状态
    NSString *supType = model.suptype;
    //超时级别
    NSString *supFlag = model.supflag;
    //1.未超时
    if ([supFlag isEqualToString:@"0"]) {
        statusStr = @"";
    }
    //2.有超时
    else{
        //2.1超时未督办
        if ([supType isEqualToString:@"000"] || [supType isEqualToString:@""]) {
            statusStr = [NSString stringWithFormat:@"%@级超时",supFlag];
        }
        //2.2超时已督办
        else{
            //先取超时级别
            NSInteger overLevel = [supFlag integerValue];
            if (supType.length < overLevel) {
                return @"";
            }
            NSString *supLevel = [supType substringWithRange:NSMakeRange(overLevel-1, 1)];
            if ([supLevel integerValue] > 0) {
                statusStr = [NSString stringWithFormat:@"%@级督办",supFlag];
            }
            else{
                statusStr = [NSString stringWithFormat:@"%@级超时",supFlag];
            }
        }
    }
    
    return statusStr;
}

@end
